/**
 * 
 */
package com.alstudio.ui.module.voip;

import java.util.ArrayList;

import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.core.voip.CallDetail;
import com.alstudio.pojo.module.user.User;
import com.alstudio.utils.android.color.ALColorUtils;
import com.alstudio.utils.formater.time.ALTimeUtils;
import com.alstudio.view.absadapter.ViewAdapter;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class CallDetailAdapter extends ViewAdapter {

	private ArrayList<CallDetail> data = new ArrayList<CallDetail>();

	/**
	 * 
	 */
	public CallDetailAdapter(ArrayList<CallDetail> data) {
		// TODO Auto-generated constructor stub
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater()
					.inflate(R.layout.call_detail_item, null);
			viewHolder.datetime_layout = (RelativeLayout) convertView
					.findViewById(R.id.datetime_layout);
			viewHolder.view_action_info = (TextView) convertView
					.findViewById(R.id.view_action_info);
			viewHolder.view_consume_info = (TextView) convertView
					.findViewById(R.id.view_consume_info);
			viewHolder.view_datetime = (TextView) convertView
					.findViewById(R.id.view_datetime);

			viewHolder.calling_time = (TextView) convertView
					.findViewById(R.id.calling_time);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.datetime_layout.setVisibility(View.GONE);

		CallDetail item = data.get(position);
		viewHolder.datetime_layout.setVisibility(View.GONE);

		if (position == 0) {
			viewHolder.datetime_layout.setVisibility(View.VISIBLE);

			viewHolder.view_datetime.setText(ALTimeUtils.formatYear(item
					.getTime() * 1000));
		} else {
			long preTime = data.get(position - 1).getTime();
			// if (preTime != item.getTime()) {
			if (!ALTimeUtils.isTheSameDay(preTime * 1000, item.getTime() * 1000)) {
				viewHolder.datetime_layout.setVisibility(View.VISIBLE);

				viewHolder.view_datetime.setText(ALTimeUtils.formatYear(item
						.getTime() * 1000));
			} else {
				viewHolder.datetime_layout.setVisibility(View.GONE);
			}
		}

		viewHolder.calling_time.setText(ALTimeUtils.formatHourMinute(item
				.getTime() * 1000));

		viewHolder.view_action_info.setText("");
		viewHolder.view_consume_info.setText("");

		if (item.isCaller()) {
			// 呼出
			viewHolder.view_action_info.setText(R.string.TxtCallOutgoing);
		} else {
			// 呼入
			viewHolder.view_action_info.setText(R.string.TxtCallincomming);
		}

		viewHolder.view_consume_info.setTextColor(ALColorUtils.getColor(
				ALLocalEnv.getLocalEnv(), R.color.call_history_calling_time));
		String txt = "";
		// 通話狀態
		switch (item.getState()) {
		case User.CALL_STATE_CANCEL:
			txt = ALLocalEnv.getLocalEnv().getString(R.string.TxtCallCancel);
			break;
		case User.CALL_STATE_MISS:
			txt = ALLocalEnv.getLocalEnv().getString(R.string.TxtMissCall);
			viewHolder.view_consume_info.setTextColor(ALColorUtils.getColor(
					ALLocalEnv.getLocalEnv(), R.color.call_history_red));
			break;
		case User.CALL_STATE_ACCEPT:
			txt = ALTimeUtils.formatVoipCallingTime(ALLocalEnv.getLocalEnv(),
					item.getCallingTime());
			break;
		case User.CALL_STATE_REJECT:
			txt = ALLocalEnv.getLocalEnv().getString(R.string.BtnReject);
			break;
		case User.CALL_STATE_CALL_NOTIFY:
			txt = ALLocalEnv.getLocalEnv().getString(R.string.TxtMissCall);
			txt += "("
					+ ALLocalEnv.getLocalEnv()
							.getString(R.string.TxtCallNotice) + ")";
			viewHolder.view_consume_info.setTextColor(ALColorUtils.getColor(
					ALLocalEnv.getLocalEnv(), R.color.call_history_red));
			break;
		}
		viewHolder.view_consume_info.setText(txt);

		return convertView;
	}

	private class ViewHolder {
		public RelativeLayout datetime_layout;
		public TextView view_action_info;
		public TextView view_consume_info;
		public TextView view_datetime;
		public TextView calling_time;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.absadapter.ViewAdapter#getRealCount()
	 */
	@Override
	public int getRealCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

}
